<template>

  <div class="app-container">

    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="停车场名称">
        <el-input v-model="formInline.name" placeholder="请输入停车场名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button type="success" @click="onAdd" v-if="userType==0 ||userType==2">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" width="110" label="序号" >
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="停车场名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="经度" width="120"  align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.longitude }}</span>
        </template>
      </el-table-column>
      <el-table-column label="纬度"  width="120" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.latitude}}</span>
        </template>
      </el-table-column>
      <el-table-column label="管理员" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.userName}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" >
        <template v-slot="scope">
          <el-button type="primary" size="mini" @click="toCar(scope.row.id)">查看停车位信息</el-button>
          <el-button type="primary" size="mini" @click="toEdit(scope.row)" v-if="userType==0 ||userType==2">编辑</el-button>
          <el-button type="primary" size="mini" @click="toGetCar(scope.row.id,scope.row.userId)">预定</el-button>
          <el-button type="danger"  size="mini" @click="toDel(scope.row.id)" v-if="userType==0 ||userType==2">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-pagination
      :page-size="pageCount"
      :page-count="pages"
      :current-page="formInline.pageNow"
      @current-change="changePageNum"
      background
      layout="prev, pager, next"
      :total="total">
    </el-pagination>

    <el-dialog
      destroy-on-close
      title="添加/编辑停车场"
      :visible.sync="dialogVisible"
      width="60%">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="停车场名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="ruleForm.longitude"></el-input>
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="ruleForm.latitude"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="userId">
          <el-select v-model="ruleForm.userId" placeholder="请选择停车场管理员">
            <el-option label="--请选择--" value=""></el-option>
            <el-option v-for="u in users" :label="u.name" :value="u.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>

</template>
<script >

import {getLotPage, saveOrUpdate, del, validate, getLotUser} from "@/api/lot";
import {getPage} from "@/api/user";
import {Message} from "element-ui";
import router from "@/router";

export default {
  name: "lot",
  data(){
    var validateLotName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入停车场名称'));
      } else {
        //发送请求
        validate({name:value,id:this.ruleForm.id}).then( res => {
          callback();

        })
      }
    };
    return {
      users:[],
      userType:'',
      uname:'',
      dialogVisible: false,
      ruleForm:{
        id: '',
        name:'',
        longitude: '',
        latitude: '',
        userId: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入停车场名称', trigger: 'blur'},
          { validator: validateLotName, trigger: 'blur' }
        ],
        longitude: [
          {required: true, message: '请输入经度', trigger: 'blur'},
        ],
        latitude: [
          {required: true, message: '请输入纬度', trigger: 'blur'}
        ],
        userId: [
          {required: true, message: '请选择车库管理员', trigger: 'blur'}
        ],
      },
      formInline:{
        pageNow:1,
        name:'',
      },
      list: [],

      pageCount: 3,
      total: 0,
      pages: 0,
      name:'',
      listLoading: true
    }
  },
  created() {
    this.userType=sessionStorage.getItem("type");
    this.uname=sessionStorage.getItem("username")
    this.fetchData()
    this.getUsers()
  },
  methods: {
    toGetCar(id,uid){
      this.$router.push({path:'/addorder/addorder',query:{lotId:id,userId:uid}})
    },
    toCar(pid){
      this.$router.push({path:'/car/car',query:{pId:pid}})
    },
    getUsers(){
      getLotUser().then(res=>{
        if(res.code===20000){
          this.users=res.data
        }
      })
    },
    onAdd(){
      this.ruleForm={}
      this.dialogVisible=true
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveOrUpdate(this.ruleForm).then( res=>{
            console.log(res)
            this.$message.success("操作成功")
            this.dialogVisible=false
            this.fetchData()
          })
        } else {
          this.$message.error("请按提示填写表单")
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toEdit(row){
      this.$set(this.ruleForm,'id',row.id)
      this.$set(this.ruleForm,'longitude',row.longitude)
      this.$set(this.ruleForm,'latitude',row.latitude)
      this.$set(this.ruleForm,'name',row.name)
      this.$set(this.ruleForm,'userId',row.userId)
      this.dialogVisible=true

    },
    toDel(id){

      this.$confirm('此操作将永久删除该停车场, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        del(id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetchData()
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    onSearch(){
      this.formInline.pageNow=1
      this.fetchData()
    },

    changePageNum(p){
      this.formInline.pageNow=p
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getLotPage(this.formInline,this.pageCount,this.uname).then(res => {
        this.list = res.data.records
        this.pages = res.data.pages
        this.formInline.pageNow= res.data.current
        this.total=res.data.total
        this.listLoading = false
      })
    }
  },


}

</script>



<style scoped>

</style>
